package com.bishe.tax.service;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxDeclaration;
import com.bishe.tax.mapper.TaxDeclarationMapper;
import com.bishe.tax.utils.Convert;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 纳税申报
 * <p>
 * <p>
 * <p>
 * <p>
 * 服务层实现
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Service
public class TaxDeclarationService {
    @Autowired
    private TaxDeclarationMapper declarationMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;
    @Autowired
    org.activiti.engine.TaskService taskservice;

    /**
     * 查询纳税申报
     * 信息
     */

    public TaxDeclaration selectDeclarationById(Integer declarationId) {
        return declarationMapper.selectDeclarationById(declarationId);
    }

    /**
     * 查询纳税申报
     * 列表
     * 集合
     */

    public List<TaxDeclaration> selectDeclarationList(TaxDeclaration declaration) {
        return declarationMapper.selectDeclarationList(declaration);
    }

    /**
     * 新增纳税申报
     *
     * @param declaration 纳税申报
     *                    <p>
     *                    <p>
     *                    <p>
     *                    <p>
     *                    信息
     * @return 结果
     */

    public int insertDeclaration(TaxDeclaration declaration) {
        return declarationMapper.insertDeclaration(declaration);
    }

    /**
     * 修改纳税申报
     *
     * @param declaration 纳税申报
     *                    <p>
     *                    <p>
     *                    <p>
     *                    <p>
     *                    信息
     * @return 结果
     */

    public int updateDeclaration(TaxDeclaration declaration) {
        return declarationMapper.updateDeclaration(declaration);
    }

    /**
     * 删除纳税申报
     * <p>
     * <p>
     * <p>
     * <p>
     * 对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteDeclarationByIds(String ids) {
        String[] ar = ids.split(",");
        for (String id : ar) {
            TaxDeclaration ia = declarationMapper.selectDeclarationById(Integer.valueOf(id));
            if (ia != null && StringUtils.isNotBlank(ia.getProcessInstanceId())) {
                runtimeservice.deleteProcessInstance(ia.getProcessInstanceId(), "");
            }
        }
        return declarationMapper.deleteDeclarationByIds(Convert.toStrArray(ids));
    }

    public int doSubmitFlow(SysUser user, String declarationId) {
        TaxDeclaration declaration = declarationMapper.selectDeclarationById(Integer.valueOf(declarationId));
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "taxDeclaration");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("singleExamine",
                declaration.getDeclarationId().toString(), variables);
        declaration.setProcessInstanceId(instance.getId());
        declaration.setStatus(2);
        return declarationMapper.updateDeclaration(declaration);
    }
}
